package com.ainiyar.springweb.controller.admin;

import com.ainiyar.springweb.common.constant.MenuConstant;
import com.ainiyar.springweb.common.result.ResultResponse;
import com.ainiyar.springweb.pojo.admin.vo.RolePermissionVo;
import com.ainiyar.springweb.pojo.entity.Menu;
import com.ainiyar.springweb.service.MenuService;
import com.ainiyar.springweb.util.ToolsUtil;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**************************************
 * File Name : MenuController
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/5  02:10
 * Desc :菜单权限管理控制器
 ***************************************/
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    /**
     * 列表
     *
     * @return List<Menu>
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('menu:list')")
    public ResultResponse<List<Menu>> lists() {
        return menuService.lists();
    }

    /**
     * 为下拉框
     *
     * @return Object
     */
    @GetMapping("/parent_menus")
    public ResultResponse<Object> getParentMenus() {
        return ResultResponse.Result(menuService.getForSelect());
    }
    /**
     * 新增菜单权限
     *
     * @param menu 新增参数
     * @return String
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('menu:add')")
    public ResultResponse<String> addSave( Menu menu) {
        return ValidateMenuAndSaveData(menu);
    }
    /**
     * 修改菜单权限
     *
     * @param menu 修改参数
     * @return String
     */
    @PutMapping("/edit")
    @PreAuthorize("hasAuthority('menu:edit')")
    public ResultResponse<String> editSave(Menu menu) {
        return ValidateMenuAndSaveData(menu);
    }
    /**
     * 删除菜单权限
     *
     * @param id 所需删除菜单权限ID
     * @return String
     */
    @DeleteMapping("/del/{id}")
    @PreAuthorize("hasAuthority('menu:del')")
    public ResultResponse<String> deleteMenu(@PathVariable Long id) {
        if (menuService.hasChildren(id)) {
            return ResultResponse.Fail(MenuConstant.DELETE_ERROR_HAS_CHILD);
        }
        if (menuService.isBindRole(id)) {
            return ResultResponse.Fail(MenuConstant.DELETE_ERROR_IS_BIND_ROLE);
        }
        if (menuService.deleteMenu(id)) {
            return ResultResponse.Success(MenuConstant.DELETE_SUCCESS);
        }
        return ResultResponse.Fail(MenuConstant.DELETE_ERROR);
    }
    /**
     * 处理新增，修改菜单数据
     * @param menu 菜单数据
     */
    private ResultResponse<String> ValidateMenuAndSaveData(Menu menu){
        if (menu.getParentId() == null) {
            return ResultResponse.Fail(MenuConstant.EMPTY_PARENT_MENU);
        }
        if (ToolsUtil.isNullOrEmpty(menu.getLabel())) {
            return ResultResponse.Fail(MenuConstant.EMPTY_LABEL);
        }
        if (ToolsUtil.isNullOrEmpty(menu.getCode())) {
            return ResultResponse.Fail(MenuConstant.EMPTY_CODE);
        }
        if (menu.getType() == 1 || menu.getType() == 2) {
            if (ToolsUtil.isNullOrEmpty(menu.getName())) {
                return ResultResponse.Fail(MenuConstant.EMPTY_NAME);
            }
        }
        if (menu.getType() == 2 && ToolsUtil.isNullOrEmpty(menu.getUrl())) {
            String url_error = menu.getELink() == 0 ?MenuConstant.EMPTY_PATH : MenuConstant.EMPTY_E_LINK;
            return ResultResponse.Fail(url_error);
        }
        String type_text = switch (menu.getType()) {
            case 1 -> "目录";
            case 2 -> "菜单";
            default -> "按钮";
        };
        if (menu.getId()!=null && menu.getId()!=0){
            boolean save = menuService.saveAddEditMenu(menu);
            if (save) {
                return ResultResponse.Success(MenuConstant.EDIT + type_text + "成功");
            }
            return ResultResponse.Fail(MenuConstant.EDIT + type_text + "失败");
        }else {
            boolean add = menuService.saveAddEditMenu(menu);
            if (add) {
                return ResultResponse.Success(MenuConstant.CREATE + type_text + "成功");
            }
            return ResultResponse.Fail(MenuConstant.CREATE + type_text + "失败");
        }
    }

    /**
     * 为角色配置权限
     */
    @GetMapping("/get_permission")
    public ResultResponse<List<RolePermissionVo>> getPermission(){
        return ResultResponse.Result(menuService.getMenuForPermission());
    }
}

